package com.eliza.v.http.base

import com.eliza.v.http.VertxTools
import io.vertx.core.AbstractVerticle
import io.vertx.core.Promise
import io.vertx.core.http.HttpServerRequest
import io.vertx.ext.web.FileUpload
import io.vertx.ext.web.Router
import io.vertx.ext.web.Router.router
import io.vertx.ext.web.handler.BodyHandler


class BodyData : AbstractVerticle() {

    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            VertxTools.runVertical(BodyData::class.java)
        }
    }

    override fun start(startPromise: Promise<Void>) {
        super.start(startPromise)
       run()
    }

    fun run() {
        val router = router(vertx)

        putGetSome(router)

    }


    /** 请求正文处理 BodyHandler
    允许您检索请求正文、限制正文大小和处理文件上传*/

    private fun putGet(router: Router) {

        router.route().handler { ctx ->
            val request: HttpServerRequest = ctx.request()

            /*如果之前需要异步调用，则HttpServerRequest应该暂停然后恢复，
                以便在主体处理程序准备好处理它们之前不会传递请求事件*/
            request.pause()


            // Resume the request
            request.resume()

            // And continue processing
            ctx.next()
        }


// This body handler will be called for all routes
        router(vertx).route().handler(BodyHandler.create())
        VertxTools.result(vertx, this, "putGet", router, 8080)

    }

    /**
     * 获取请求正文
     */
    private fun putGetSome(router: Router) {
        /**
         * 按声明顺序进行匹配
         */
        val router = router(vertx)
        router.post("/putGetSome").handler { rc ->
            println(rc.request().getParam("name"))
            /*       rc.request().bodyHandler { body ->
                       println(body.toString())


                       var responseData = JsonObject()
                           .put("msg", "success");
                       rc.response().end(responseData.toString());
                   }*/
            rc.response().end("sssss");

        }
        VertxTools.result(vertx, this, "putGetSome", router, 8080)


    }

}